//
//  Clnetwork.m
//  AF
//
//  Created by 曹亮 on 2017/2/28.
//  Copyright © 2017年 曹亮. All rights reserved.
//

#import "Clnetwork.h"

@implementation Clnetwork
//单例
+(instancetype)shareManager
{
    static Clnetwork *manager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
//        manager = [[self alloc]initWithBaseURL:[NSURL URLWithString:@"http://httpbin.org/"]];
        
        manager =[[Clnetwork alloc]init];
        
    });
    
    return manager;
}


-(instancetype)initWithBaseURL:(NSURL *)url
{
    self = [super initWithBaseURL:url];
    
    if (self) {
        //请求超时设定
        
        self.requestSerializer.timeoutInterval = 5;
        
        self.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringCacheData;
        [self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [self.requestSerializer setValue:url.absoluteString forHTTPHeaderField:@"Referer"];
        
        self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html", nil];
        
        self.securityPolicy.allowInvalidCertificates = YES;
    }
    return self;}

-(void)requestWithMethod:(HTTPMethod)method WithPath:(NSString *)path WithParas:(NSDictionary *)paras WithSuccessBlock:(requestSuccessBlock)success WithfailureBlock:(requestFailureBlock)failure
{
    switch (method) {
        case GET:
        {
        [self GET:path parameters:paras progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"json%@",responseObject);
            
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
           
            NSLog(@"error%@",error);
            
            failure(error);
            
        }];
        
            
            
            
            break;
        }
            
            case POST:
        {
        [self POST:path parameters:paras progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSLog(@"%@",responseObject);
            NSLog(@"%@",responseObject);
            NSLog(@"%@",responseObject);
            NSLog(@"%@",responseObject);
            NSLog(@"%@",responseObject);
            
            success(responseObject);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
            
            failure(error);
        }];
        
        }
        default:
            break;
    }
}


@end
